using Microsoft.EntityFrameworkCore;
using community.Models;

namespace community.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Resident> Residents { get; set; }
        public DbSet<House> Houses { get; set; }
        public DbSet<PropertyFee> PropertyFees { get; set; }
        public DbSet<PropertyEquipment> PropertyEquipments { get; set; }
        public DbSet<CommunityEvent> CommunityEvents { get; set; }
        public DbSet<EventRegistration> EventRegistrations { get; set; }
        public DbSet<Complaint> Complaints { get; set; }
        public DbSet<Permission> Permissions { get; set; }


        public DbSet<HouseComplex> HouseComplex { get; set; }
        public DbSet<Committee> Committee { get; set; }
        public DbSet<Complex> Complex { get; set; }
        public DbSet<ComplexCommittee> ComplexCommittee { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置User表
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasIndex(e => e.Username).IsUnique();
                entity.HasIndex(e => e.Email).IsUnique();
                entity.Property(e => e.Role).HasDefaultValue("User");
            });

            // 配置House表
            modelBuilder.Entity<House>(entity =>
            {
                entity.HasIndex(e => new { e.BuildingNo, e.UnitNo, e.RoomNo }).IsUnique();
                
                // 配置与Resident的关系
                entity.HasOne(e => e.Owner)
                    .WithMany()
                    .HasForeignKey(e => e.OwnerId)
                    .OnDelete(DeleteBehavior.SetNull);
            });

            // 配置EventRegistration表
            modelBuilder.Entity<EventRegistration>(entity =>
            {
                entity.HasIndex(e => new { e.EventId, e.UserId }).IsUnique();
            });

            // 配置PropertyFee和House之间的关系
            modelBuilder.Entity<PropertyFee>(entity =>
            {
                entity.HasOne(e => e.House)
                    .WithMany()
                    .HasForeignKey(e => e.HouseId)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            // 配置Committee表
            modelBuilder.Entity<Committee>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).ValueGeneratedOnAdd();
                entity.Property(e => e.Name).IsRequired();
                entity.Property(e => e.Address).IsRequired();
                entity.Property(e => e.ContactPhone).IsRequired();
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("GETUTCDATE()");
                entity.Property(e => e.UpdatedAt).HasDefaultValueSql("GETUTCDATE()");
            });

            //// 配置Complex表
            //modelBuilder.Entity<Complex>(entity =>
            //{
            //    entity.Property(e => e.Address).IsRequired();
            //    entity.Property(e => e.CreatedAt).HasDefaultValueSql("GETUTCDATE()");
            //    entity.Property(e => e.UpdatedAt).HasDefaultValueSql("GETUTCDATE()");
                
            //    // 配置与Committee的关系
            //    entity.HasOne(e => e.Committee)
            //        .WithMany(c => c.Complexes)
            //        .HasForeignKey(e => e.CommitteeId)
            //        .OnDelete(DeleteBehavior.Restrict);
            //});

            //// 配置Committee表
            //modelBuilder.Entity<Committee>(entity =>
            //{
            //    entity.HasMany(c => c.Complexes)
            //        .WithOne(e => e.Committee)
            //        .HasForeignKey(e => e.CommitteeId)
            //        .OnDelete(DeleteBehavior.Restrict);
            //});

            // 其他表的配置...
        }
    }
} 